﻿using Abp;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using AutoMapper.QueryableExtensions;
using FwProject.Authorization.Users;
using FwProject.Com.Dto;
using FwProject.PublicService;
using FwProject.ShopApplication;
using FwProject.ShoppingMall.Com;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X.PagedList;
using Abp.UI;
namespace FwProject.Com
{
    public class FeedbackAppService : FwProjectAppServiceBase, IFeedbackAppService
    {
        private readonly IRepository<Feedback, Guid> _feedbackRepository;
        private readonly IRepository<User,long> _userRepository;

        private readonly IConfigurationRoot _appConfiguration;
        private Abp.IGuidGenerator _guid { get; set; }
        public FeedbackAppService(IRepository<Feedback, Guid> feedbackRepository, IHostingEnvironment hostingEnvironment, IRepository<User, long> userRepository)
        {
            _feedbackRepository = feedbackRepository;
            _appConfiguration = hostingEnvironment.GetAppConfiguration();
            _guid = SequentialGuidGenerator.Instance;
            _userRepository = userRepository;
        }
        #region
        [AbpAuthorize]
        public async Task CreateFeedback(FeedbackInput input) {
            if (AbpSession.UserId.HasValue)
            {
                var model = input.MapTo<Feedback>();
                model.Id = _guid.Create();
                model.UserId = AbpSession.UserId.Value;
                await _feedbackRepository.InsertAndGetIdAsync(model);
            }
            else {

                new UserFriendlyException(120, "请登录");
            }
          
        }

        #endregion

        #region 后台管理

        /// <summary>
        /// 后台管理-获取Layui数据
        /// </summary>
        /// <param name="page">当前页</param>
        /// <param name="limit">每页数据</param>
        /// <param name="filter">查询</param>
        /// <returns></returns>
        [AbpAuthorize]
        public async Task<LayerDataBaseDto> GetFeedbackLayerDataAsync(int page, int limit, FeedbackFilter filter)
        {
            var query = _feedbackRepository.GetAll();

            if (filter!=null)
            {
                if (!string.IsNullOrWhiteSpace(filter.keyword))
                    query = query.Where(o => o.Phone.Contains(filter.keyword));

                if (filter.QuestionType.HasValue)
                {
                    query = query.Where(o => o.QuestionType == filter.QuestionType);
                }
            }
        

            var pagelist = await query.OrderByDescending(o => o.CreationTime).ProjectTo<FeedbackOutputDto>().ToPagedListAsync(page, limit);

            foreach (var item in pagelist)
            {
                item.UserName = (await _userRepository.GetAsync(item.UserId)).UserName;
            }
            LayerDataBaseDto dto = new LayerDataBaseDto();
            dto.Data = pagelist;
            dto.Count = pagelist.TotalItemCount;
            return dto;
        }


        /// <summary>
        /// 后台管理-获取实体
        /// </summary>
        /// <param name="id">意见反馈Id</param>
        /// <returns></returns>
        [AbpAuthorize]
        public async Task<FeedbackOutputDto> GetEntityAsync(Guid id)
        {
            var entity =await _feedbackRepository.GetAsync(id);

            var dto = entity.MapTo<FeedbackOutputDto>();
            dto.UserName= (await _userRepository.GetAsync(entity.UserId)).UserName;

            dto.ImgaList = dto.Imgs.Split('|').ToList();
            return dto;

        }


        /// <summary>
        /// 后台管理-删除
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        [AbpAuthorize]
        public async Task DeleteAsync(Guid id)
        {
            var entity = await _feedbackRepository.GetAsync(id);
            await _feedbackRepository.DeleteAsync(entity);
        }


        #endregion
    }
}
